<?php
class XMysqlDriver extends XAbstractDBDriver{
	protected $extension = 'mysql';
	
	public function connect(){
		if($this->connected
			&& $this->connection){
			return $this->connection;
		}		
		$this->host = $this->config['host'];
		$this->port = isset($this->config['port']) ? $this->config['port'] : '3306';
		$server = $this->host.':'.$this->port;
		if(isset($this->config['persist'])
			&& strtolower($this->config['persist']=='true')){
			$this->connection = mysql_pconnect($server, $this->config['username'], $this->config['password']);	
		}else{
			$this->connection = mysql_connect($server, $this->config['username'], $this->config['password']);	
		}
		$this->_handleError();
		mysql_select_db($this->config["database"], $this->connection);	
		$this->_handleError();
		if(isset($this->config["charset"])){
			mysql_set_charset($this->config["charset"], $this->connection);
			$this->_handleError();
		}
		XEventContext::singleton()->publish(new XLoggingEvent($this, INFO, "MySQL Database {$this->host}:{$this->port} Connected."));
		$this->connected = true;
		return $this->connection;
	}
	
	public function close(){
		if($this->connected
			&& is_resource($this->connection)){
			mysql_close($this->connection);
		}
		$this->connection = null;
		$this->connected = false;
		XEventContext::singleton()->publish(new XLoggingEvent($this, INFO, "MySQL Database {$this->host}:{$this->port} Closed."));
	}
	
	public function exec($sql){
		$query = $this->_query($sql);
		$rows = mysql_affected_rows($this->connect());
		$this->_free($query);
		return $rows;
	}
	
	public function query($sql){
		$query = $this->_query($sql);
		$array = array();
		while($row = mysql_fetch_assoc($query)){
		    $array[] = $row;
		}
		$this->_free($query);
		return $array;
	}
	
	public function lastId(){
		return mysql_insert_id($this->connect());
	}
	
	public function escape($str){
		if(is_null($str)){
			return "NULL";
		}
		return mysql_real_escape_string($str, $this->connect());
	}
	
	public function offsetKeyword(){
		return 'OFFSET';
	}
	
	public function sizeKeyword(){
		return 'LIMIT';
	}
		
	private function _query($sql){
		$query = mysql_query($sql, $this->connect());
		$this->_handleError();
		return $query;
	}
	
	public function error(){
		if(!$this->connection){
			return null;
		}
		return mysql_error($this->connection);
	}
	
	public function hasError(){
		$error = $this->error();
		return !is_null($error) && strlen($error) > 2;
	}
	
	private function _handleError(){
		$error = $this->error();
		if($error){
			throw new XORMException($error);
		}
	}
	
	private function _free($query){
		if(gettype($query) == 'resource'){			
			mysql_free_result($query);
		}
	}	
	
	public function version(){
		$result = $this->query("select version();");
		return x_array_head(array_values(x_array_head($result)));
	}
	
	public function rmUID(){
		return 'mysql://'.$this->host.':'.$this->port;
	}
	
	protected function versionSupportsXA(){
		return floatval($this->version()) >= 5.0;
	}	
	
}
?>